import Taro from '@tarojs/taro'
import { UPDATE_CABINETS_LIST, CABINET_DETAIL_PENDING, CABINET_DETAIL_RESOLVE, CABINET_DETAIL_REJECT} from './../constants/cabinets';
import { URL_FIND_CABINETS } from './../constants/SystemConstants';
import { WXRequest } from './../utils/WXRequest';

export const findCabinets = () => async dispatch => {
  let map = Taro.createMapContext('ax-cabinet-map')
  map.getRegion({
    success: (res => {
      WXRequest(URL_FIND_CABINETS, 'GET', {currLng: res.southwest.longitude, currLat: res.northeast.latitude}).then(res => {
        dispatch({
          type: UPDATE_CABINETS_LIST,
          cabinets: res.data
        })
      }).catch(e => {
        console.error(e)
      })
    })
  })
}

export const getCabinets = (id) => async dispatch => {
  dispatch({
    type: CABINET_DETAIL_PENDING
  })
  WXRequest(URL_FIND_CABINETS, 'GET', {id : id}).then(res=> {
    dispatch({
      type: CABINET_DETAIL_RESOLVE,
      cabinet: res.data[0]
    })
  }).catch(e => {
    console.error(e)
    dispatch({
      type: CABINET_DETAIL_REJECT
    })
  })
}